package com.net.rip;

import com.net.rip.entity.Network;
import com.net.rip.utils.netInit;
import com.net.rip.utils.netUpdate;
import com.net.rip.utils.outPut;
import com.net.rip.utils.tableUpdate;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

@SpringBootApplication
public class RipApplication {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Network network = new Network();

        int chosen; //输入的选项
        Scanner scanner = new Scanner(System.in);

        boolean flag = false;

        // RIP定时更新
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (network.getRouterNum() == 0) {
                    System.out.println("未初始化，请先完成初始化！\n");
                } else {
                    try {
                        tableUpdate.tableupdate(network);
                        System.out.println("**定时交换路由信息完成**");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        // 计时器
        Timer timer = new Timer();
        // 开始执行任务 (延迟30s执行，每30秒执行一次)
        timer.schedule(timerTask, 30000, 30000);

        while (true) {
            System.out.println("**********菜单**********");
            System.out.println("1.初始化");
            System.out.println("2.显示当前各路由表信息");
            System.out.println("3.根据循环轮次更新路由表");
            System.out.println("4.更改网络拓扑结构");
            System.out.println("5.结束");
            System.out.println("************************");
            System.out.println("请选择：");
            chosen = scanner.nextInt();

            switch (chosen) {
                case 1:
                    netInit.netinit(network);
                    flag = true;
                    break;

                case 2:
                    if (!flag) {
                        System.out.println("未初始化，请先完成初始化！\n");
                    } else {
                        outPut.output(network);
                    }
                    break;

                case 3:
                    if (!flag) {
                        System.out.println("未初始化，请先完成初始化！\n");
                    } else {
                        tableUpdate.doUpdate(network);
                    }
                    break;

                case 4:
                    if (!flag) {
                        System.out.println("未初始化，请先完成初始化！\n");
                    } else {
                        netUpdate.doCycle(network);
                    }
                    break;

                case 5:
                    System.exit(0);
                    break;
            }

        }


    }

}
